package com.zhou.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhou.dto.vip.VipDTO;
import com.zhou.entity.Admin;
import com.zhou.entity.Vip;
import com.zhou.mapper.AdminMapper;
import com.zhou.mapper.VipMapper;
import com.zhou.service.VipService;
import com.zhou.utils.APPApplicationException;
import com.zhou.utils.BeanChangeUtils;
import com.zhou.utils.enums.BooleanEnum;
import com.zhou.utils.enums.VIPTimeEnum;
import com.zhou.vo.vip.VIPVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.*;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @Description : 购买vip业务层
 * @Author : zyy
 * @Date: 2024/6/19  15:05
 */
@Service
@Slf4j
public class VipServiceImpl implements VipService {
    @Resource
    private VipMapper vipMapper;

    @Resource
    private AdminMapper adminMapper;

    @Override
    public String add(VipDTO dto) {
        Vip vip = new Vip();
        //购买vip，需要判断是否有买过vip
        Admin admin = adminMapper.selectOne(new LambdaQueryWrapper<Admin>().eq(Admin::getId, dto.getUserInfoId()));
        if (BeanChangeUtils.isNotEmpty(admin)) {
            if (BeanChangeUtils.isNotEmpty(admin.getVipId())) {
                //如果不为空，就查询vip
                vip = vipMapper.selectOne(new LambdaQueryWrapper<Vip>().eq(Vip::getVidId, admin.getVipId()));
                if (vip.getIsStatus().equals(BooleanEnum.TURE.getResultValue())) {
                    throw new APPApplicationException("账号的VIP已经被禁用，请联系管理员解除后继续操作");
                }
                //已经购买了vip
                if (BeanChangeUtils.isEmpty(vip.getExpirationTime())) {
                    //如果没有过期时间就是永久vip
                    throw new APPApplicationException("您已经购买了永久的vip，无需重复购买");
                }
                //不是永久vip需要判断是否过期
                if (vip.getExpirationTime().getTime() < System.currentTimeMillis()) {
                    //已经过期了，就将现在的系统时加上购买的时间
                    if (dto.getExpirationTime().equals(VIPTimeEnum.PERMANENT.getDays())) {
                        //购买永久
                        vip.setExpirationTime(this.setDate());
                    } else {
                        //有时间限制
                        Date date = this.ChangeDate(dto.getExpirationTime());
                        log.info("时间是" + date);
                        vip.setExpirationTime(date);
                    }
                } else {
                    //如果没过期,且买的是永久的
                    if (dto.getExpirationTime().equals(VIPTimeEnum.PERMANENT.getDays())) {
                        vip.setExpirationTime(this.setDate());
                    } else {
                        Date date = this.OldChangeDate(dto.getExpirationTime(), vip.getExpirationTime());
                        log.info("时间是" + date);
                        vip.setExpirationTime(date);
                    }
                }
                vip.setDescribes(dto.getDescribes());
                int i = vipMapper.updateById(vip);
                if (BooleanEnum.FLASE.getResultValue().equals(i)) {
                    throw new APPApplicationException("更新VIP过期时间失败");
                }
            } else {
                //如果没有买过vip
                if (dto.getExpirationTime().equals(VIPTimeEnum.PERMANENT.getDays())) {
                    //购买永久
                    vip.setExpirationTime(this.setDate());
                } else {
                    //有时间限制
                    Date date = this.ChangeDate(dto.getExpirationTime());
                    log.info("时间是" + date);
                    vip.setExpirationTime(date);
                }
                vip.setVidId(UUID.randomUUID().toString());
                vip.setDescribes(dto.getDescribes());
                int insert = vipMapper.insert(vip);
                if (insert > BooleanEnum.FLASE.getResultValue()) {
                    //如果大于零就说明新增vip成功，需要与用户绑定
                    admin.setVipId(vip.getVidId());
                    adminMapper.updateById(admin);
                } else {
                    return "新增失败";
                }
            }
        } else {
            return "用户不存在";
        }
        return "购买成功";
    }

    /**
     * VIP分页查询
     *
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public List<VIPVO> list(Integer page, Integer pageSize) {

        List<VIPVO> list = vipMapper.getList(page, pageSize);
        return list;
    }

    /**
     * vip状态变更
     *
     * @param id
     * @return
     */
    @Override
    public String stop(String id) {
        Vip vip = vipMapper.selectById(id);
        if (BeanChangeUtils.isEmpty(vip)) {
            return "vip不存在";
        }
        if (vip.getIsStatus().equals(BooleanEnum.TURE.getResultValue())) {
            vip.setIsStatus(BooleanEnum.FLASE.getResultValue());
            vipMapper.updateById(vip);
            return "禁用成功";
        } else {
            vip.setIsStatus(BooleanEnum.TURE.getResultValue());
            vipMapper.updateById(vip);
            return "启用成功";
        }
    }

    /**
     * 公共方法，时间相加转换
     */
    private Date ChangeDate(Integer days) {
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime localDateTime = now.plusMonths(days);
        ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneId.systemDefault());
        Instant instant = zonedDateTime.toInstant();
        return Date.from(instant);
    }

    /**
     * 原有的时间加上数月
     */
    public Date OldChangeDate(Integer days, Date originalDate) {
        ZonedDateTime zonedDateTime = originalDate.toInstant().atZone(ZoneId.systemDefault());
        ZonedDateTime dateTime = zonedDateTime.plusMonths(days);
        Instant instant = dateTime.toInstant();
        return Date.from(instant);
    }

    /**
     * 设置一个2099年的时间
     */

    public Date setDate() {
        // 使用LocalDate设置日期
        LocalDate localDate = LocalDate.of(2099, 1, 1); // 2099年1月1日

        // 转换为ZonedDateTime并指定时区
        ZonedDateTime zonedDateTime = localDate.atStartOfDay(ZoneId.systemDefault());

        // 转换为Date对象
        Date date = Date.from(zonedDateTime.toInstant());
        System.out.println("2099年1月1日对应的Date对象: " + date);
        return date;

    }
}
